/**
 * 推广组划相关api
 */
angular.module('imsApp').factory('PromotionGroupManager', ['$http','api', 'paramsManager', function ($http,api, paramsManager) {
    var PromotionGroupManager = {
        /**
         * 推广组列表
         params:[pageNo,pageSize]//顺序不能错
         */
        promotionGroupList: function (campaginId, params) {
            return $http.get(api+'/ims/campaigns/'+campaginId+'/groups/' + paramsManager.params(params)).then(function (result) {
                return result;
            });
        },
        /**
         *推广组添加
         * @param params
         * @returns {"c":"s","campaignId":1}?{"c":"f", "i":"添加失败"}
         */
        promotionGroupAdd: function (campaignId,params) {
            return $http.post(api+'/ims/campaigns/'+campaignId+'/groups', params).then(function (result) {
                return result;
            });
        },
        /**
         *推广组详情
         * @param groupId
         */
        promotionGroupDetail: function (groupId) {
            return $http.get(api+'/ims/groups/'+groupId).then(function (result) {
                return result;
            });
        },/**
         *推广组删除
         * @param groupIds [];
         */
        promotionGroupDelete: function (groupIds) {
            return $http.delete(api+'/ims/groups/'+groupIds.join(",")).then(function (result) {
                return result;
            });
        },
        /**
         *推广组修改
         * @param params
         * @returns {"c":"f"} ?{"c": "f","i":"修改失败"}
         */
        promotionGroupEdit: function (groupId,params) {
            return $http.put(api+'/ims/groups/'+groupId,params).then(function (result) {
                return result;
            });
        },
        /**
         *推广组修状态修改
         * @param params groupId,{status:11}
         * @returns {"c":"f"} ?{"c": "f","i":"修改失败"}
         */
        promotionGroupStatusEdit: function (groupId,params) {
            return $http.put(api+'/ims/groups/'+groupId+'/status',params).then(function (result) {
                return result;
            });
        },
        /**
         *推广组修统计
         * @param params [groupId,pageNo,pageSize] {timeType:day}
         */
        promotionGroupStatistics: function (params,query) {
            return $http.get(api+'/ims/groupStats/'+paramsManager.params(params)+paramsManager.query(query)).then(function (result) {
                return result;
            });
        },
        /**
         *加载现有推广组
         * @param params [pageNo,pageSize]
         */
        promotionGroupAll: function (params,query) {
            return $http.get(api+'/ims/groups/'+paramsManager.params(params)+paramsManager.query(query)).then(function (result) {
                return result;
            });
        },
        initRegion:function ($scope,$stateParams) {
            var initData = $scope.initData.initCode.targetRegional;
            var targetRegionalFormart = $scope.formData.targetRegional;//选中谁就赋给右侧展示框变量
            var data = { "countryItem": [] , "provinceItem": [] , "cityItem": [] };
            //targetRegionalFormart存在
            if(targetRegionalFormart != null && targetRegionalFormart != ""){
                //长度为0说明是选中所有国家
                if(targetRegionalFormart.length==0){
                    for(var i = 0; i < initData.length; i++) {//遍历国家            			
                        var country = initData[i];
                        data.countryItem.push(country[0]);
                        for(var k = 0; k < country[1].length; k++) {//遍历省
                            var provinceItem = country[1][k];
                            data.provinceItem.push(provinceItem[0]);
                        }
                    }
                }else{
                    for(var i = 0; i < targetRegionalFormart.length; i++) {
                        for(var j = 0; j < initData.length; j++) {
                            var country = initData[j];
                            //遍历国家比较是否相等
                            if(targetRegionalFormart[i] == country[0]) {
                                data.countryItem.push(country[0]);
                                $scope.halfCityData[country[0]] = false;//去掉部分选中样式
                                for(var k = 0; k < country[1].length; k++) {
                                    var provinceItem = country[1][k];
                                    data.provinceItem.push(provinceItem[0]);
                                    data.cityItem.push(provinceItem);
                                    $scope.halfCityData[provinceItem[0]] = false;//去掉部分选中样式
                                }
                            } else {
                                //再遍历该国家的省是否相等
                                for(var k = 0; k < country[1].length; k++) {
                                    var provinceItem = country[1][k];
                                    //与该省相等：
                                    if(targetRegionalFormart[i] == provinceItem[0]) {
                                        if(data.countryItem.indexOf(country[0]) == -1) {
                                            data.countryItem.push(country[0]);
                                        }
                                        data.provinceItem.push(provinceItem[0]);
                                        data.cityItem.push(provinceItem);
                                        $scope.halfCityData[country[0]] = true;//部分选中样式
                                        $scope.halfCityData[provinceItem[0]] = false;//去掉部分选中样式
                                        //不与该省相等，则再查看是否与下面的市级相等
                                    } else {
                                        if(provinceItem[1]) {
                                            //遍历市级
                                            for(var l = 0; l < provinceItem[1].length; l++) {
                                                var cityItem = provinceItem[1][l];

                                                if(targetRegionalFormart[i] == cityItem) {
                                                    $scope.halfCityData[country[0]] = true;//部分选中样式
                                                    $scope.halfCityData[provinceItem[0]] = true;//去掉部分选中样式
                                                    if(data.countryItem.indexOf(country[0]) == -1) {
                                                        data.countryItem.push(country[0]);
                                                    }
                                                    if(data.provinceItem.indexOf(provinceItem[0]) == -1) {
                                                        data.provinceItem.push(provinceItem[0]);
                                                        data.cityItem.push([provinceItem[0],[cityItem]]);
                                                    } else {
                                                        // data.cityItem[data.provinceItem.indexOf(provinceItem[0])][1].push(cityItem);
                                                        var city = data.cityItem;
                                                        for(var p=0;p<city.length;p++){
                                                            if(city[0] == provinceItem[0]){
                                                                data.cityItem[p][1].push(cityItem);
                                                                break;
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }

                }

            }else{//也是全选
                //详情全选
                if($stateParams.groupId){
                    for(var i = 0; i < initData.length; i++) {//遍历国家
                        var country = initData[i];
                        data.countryItem.push(country[0]);
                        for(var k = 0; k < country[1].length; k++) {//遍历省
                            var provinceItem = country[1][k];
                            data.provinceItem.push(provinceItem[0]);
                        }
                    }
                }

            }

            return data;
        },
        checkRegion:function ($scope,$stateParams,$event,target,id,the) {
            var checkbox = $event.target;
            var action = (checkbox.checked ? 'add' : 'remove');
            var area ,data;

            //添加区域
            if(action == 'add' && $scope.formData[target].indexOf(id) == -1){
                //市级别选择
                if(angular.isDefined(the.cityItem)) {
                    var sign = true;
                    var itemId;

                    //判断同级别市，是否存在未选择，如果有未选择，sign赋值false
                    for(var i = 0; i < the.provinceItem[1].length; i++) {
                        itemId = the.provinceItem[1][i];
                        if($scope.formData[target].indexOf(itemId) == -1 && itemId != id) {
                            sign = false;
                            break;
                        };
                    }

                    //sign为true，同级别市全选了，删除市，判断省
                    if(sign) {
                        $scope.halfCityData[the.provinceItem[0]] = false;
                        for(var i = 0; i < the.provinceItem[1].length; i++) {
                            itemId = the.provinceItem[1][i];
                            if(itemId != id) {
                                var idx = $scope.formData[target].indexOf(itemId);
                                $scope.formData[target].splice(idx, 1);
                            };
                        };
                    } else {
                        $scope.halfCityData[the.countryItem[0]] = true;
                        $scope.halfCityData[the.provinceItem[0]] = true;
                        $scope.formData[target].push(id);
                    }
                    area = "cityItem";
                    data = [the.provinceItem[0],[id]];
                }

                //市级别选择后续操作、省级别判断选择
                if(angular.isDefined(the.provinceItem)) {
                    //$scope.halfCityData[id] = false;
                    //sign未定义，或者sign为true，说明选中该省
                    if(angular.isUndefined(sign) || sign) {
                        $scope.halfCityData[the.provinceItem[0]] = false;
                        var sign = true;
                        var itemId;

                        //判断同级别省，是否存在未选择，如果有未选择，sign赋值false
                        for(var i = 0; i < the.countryItem[1].length; i++) {
                            itemId = the.countryItem[1][i][0];
                            if($scope.formData[target].indexOf(itemId) == -1 && itemId != the.provinceItem[0]) {
                                sign = false;
                                break;
                            };
                        }

                        //sign为true，同级别省不存在未选择，删除全部已选择省，添加国家
                        //sign为false，删除已选择市，添加省
                        if(sign) {
                            $scope.halfCityData[the.countryItem[0]] = false;

                            for(var i = 0; i < the.countryItem[1].length; i++) {
                                itemId = the.countryItem[1][i][0];

                                if(itemId != the.provinceItem[0]) {
                                    var idx = $scope.formData[target].indexOf(itemId);
                                    $scope.formData[target].splice(idx, 1);
                                };
                            }
                            $scope.formData[target].push(the.countryItem[0]);
                        } else {
                            $scope.halfCityData[the.countryItem[0]] = true;
                            $scope.formData[target].push(the.provinceItem[0]);
                            if(angular.isUndefined(the.cityItem) && typeof the.provinceItem[1] == "object") {
                                for(var i = 0; i < the.provinceItem[1].length; i++) {
                                    itemId = the.provinceItem[1][i];
                                    var idx = $scope.formData[target].indexOf(itemId);

                                    if(idx != -1) {
                                        $scope.formData[target].splice(idx, 1);
                                    };
                                };
                            };
                        };
                    }
                    if(angular.isUndefined(area)) {
                        area = "provinceItem";
                        data = the.provinceItem;
                    };
                }

                //国家级别选择
                if(angular.isUndefined(the.provinceItem) && angular.isUndefined(the.cityItem)) {
                    $scope.halfCityData[id.toString()] = false;
                    for(var i=0; i<the.countryItem[1].length;i++){
                        $scope.halfCityData[the.countryItem[1][i][0]] = false;
                    }
                    $scope.formData[target] = [];
                    $scope.formData[target].push(id);
                    area = "countryItem";
                    data = id;
                };
            }

            //删除选择区域
            if(action == 'remove'){//console.log("remvoe")
                //formData中存在当前省市或国家，直接删除
                if($scope.formData[target].indexOf(id) != -1) {
                    var idx = $scope.formData[target].indexOf(id);
                    $scope.formData[target].splice(idx,1);
                    //删除的是市级别
                    if(angular.isDefined(the.cityItem)) {//console.log("删除市")
                        var sign = true;
                        var itemId;
                        //判断同级别市，是否存在选择的，是sign赋值false
                        for(var i = 0; i < the.provinceItem[1].length; i++) {
                            itemId = the.provinceItem[1][i];
                            if($scope.formData[target].indexOf(itemId) != -1) {
                                sign = false;
                                break;
                            };
                        }
                        //sign为true，同级别市不存在未选择，删除市，判断省
                        if(sign) {
                            $scope.halfCityData[the.provinceItem[0]] = false;
                            $scope.halfCityData[the.countryItem[0]] = false;
                        }else{
                            $scope.halfCityData[the.countryItem[0]] = true;
                        }
                        //省级别删除
                    }else{//console.log("删除省")
                        var sign = true;
                        var itemId;
                        //判断同级别省，是否存在选择项，有sign赋值false
                        for(var i = 0; i < the.countryItem[1].length; i++) {
                            itemId = the.countryItem[1][i][0];
                            if($scope.formData[target].indexOf(itemId) != -1 ) {
                                sign = false;
                                break;
                            };
                        }
                        if(sign){
                            $scope.halfCityData[the.countryItem[0]] = false;
                        }else{
                            $scope.halfCityData[the.countryItem[0]] = true;
                        }

                    }

                    //不存在
                } else {
                    var isCity = false;
                    //市级别删除选择
                    if(angular.isDefined(the.cityItem)) {//console.log("删除市——不存在情况")
                        $scope.halfCityData[the.countryItem[0]] = false;
                        isCity = true;
                        var cidx = $scope.formData[target].indexOf(the.countryItem[0]);
                        var itemId = the.provinceItem[0];
                        var idx = $scope.formData[target].indexOf(itemId);
                        //对应国家选中选中该市不存的情况:删除该国家，添加其他省和该省下的其他市
                        if( cidx != -1){
                            //省下只有一个市的情况
                            if(the.provinceItem[1].length == 1){
                                $scope.halfCityData[the.provinceItem[0]] = false;
                            }else{
                                $scope.halfCityData[the.provinceItem[0]] = true;
                            }

                            $scope.formData[target].splice(cidx,1);

                            var province = the.countryItem[1];
                            for(var m = 0 ; m < province .length ; m++ ){
                                if(itemId != province[m][0]){
                                    $scope.formData[target].push(province[m][0]);
                                }
                            }

                            var city = the.provinceItem[1];
                            for(var m = 0; m < city.length ; m++){
                                if(id != city[m]){
                                    $scope.formData[target].push(city[m]);
                                }
                            }

                            //对应省全选中的情况：删除该省，添加剩余同省的城市                        	
                        }else if(idx != -1){
                            $scope.halfCityData[the.provinceItem[0]] = false;
                            $scope.formData[target].splice(idx,1);
                            for(var i = 0; i < the.provinceItem[1].length; i++) {
                                itemId = the.provinceItem[1][i];
                                if(itemId != id) {
                                    $scope.formData[target].push(itemId);
                                };
                            };
                        }

                    }
                    //市级别删除选择后续操作formData中不存在对应省级别、省级别删除选择。
                    if(angular.isDefined(the.provinceItem) && !isCity) {//console.log("删除省——不存在情况")
                        var idx = $scope.formData[target].indexOf(the.countryItem[0]);
                        //国家选中造成的省不存在，删除国家，添加其他省，
                        if( idx != -1){
                            $scope.formData[target].splice(idx,1);
                            $scope.halfCityData[the.countryItem[0]] = true;
                            $scope.halfCityData[the.provinceItem[0]] = false;
                            var province = the.countryItem[1];
                            for(var m = 0 ; m < province .length ; m++ ){
                                if(id != province[m][0]){
                                    $scope.formData[target].push(province[m][0]);
                                }
                            }
                        }

                    };
                };
            }
            $scope.ctrlData.targetRegionalFormart = this.initRegion($scope,$stateParams);
        },
        checkParent:function ($scope,$event,target,id,the) {
            //后台需要数据：当父级选中（全选）时，只发送父ID到后台；当部分子级选中时，只发选中的子级ID
            var checkbox = $event.target;
            var action = (checkbox.checked ? 'add' : 'remove');
            var pdx = $scope.targetTag.indexOf(id);//在前端数组中的索引
            //父级选中
            if(action == 'add' && pdx == -1){
                $scope.halfTagData[id] = false;
                $scope.targetTag.push(id);
                var children = the.parentItem.list;
                var isExsit = false;
                //判断已选框结果是否存在，并取出索引赋给e
                var e=0;
                for(var m=0;m<$scope.tagSelected.length;m++){
                    if($scope.tagSelected[m].tagId == the.parentItem.tagId){
                        e=m;
                        isExsit = true;
                        break;
                    };
                }

                //
                for(var i=0;i<children.length;i++){
                    if($scope.targetTag.indexOf(children[i].tagId) == -1){
                        $scope.targetTag.push(children[i].tagId);//子集标签全选中，即将子集的value全赋进$scope.targetTag里
                        //由此可知，checkbox框的回写，是将对应input框的name属性名字对应的数组含有该value值即可。                			
                    }
                    var dx = $scope.formData[target].indexOf(children[i].tagId);
                    if(dx != -1){//删除后台数组中的ID
                        $scope.formData[target].splice(dx,1);
                    }
                    //已选结果子级
                    var c = {};
                    c.tagId = children[i].tagId;
                    c.tagName = children[i].tagName;
                    if(isExsit){
                        var flag = false;
                        for(var k=0;k<$scope.tagSelected[e].list.length;k++){
                            if($scope.tagSelected[e].list[k].tagId == c.tagId){
                                flag =true;
                            };
                        }
                        //存在的话，不需再添加，继续循环
                        if(flag){
                            continue;
                            //不存在的话就
                        }else{
                            $scope.tagSelected[e].list.push(c);
                        };
                    }else{
                        //已选结果父级
                        var p = {};
                        p.tagId = id;
                        p.tagName = the.parentItem.tagName;
                        p.list = [];
                        p.list.push(c);
                        $scope.tagSelected.push(p);
                        e=$scope.tagSelected.length-1;
                        isExsit = true;
                    };

                }
                //将该ID添加到后台数组中
                if($scope.formData[target].indexOf(id) == -1){
                    $scope.formData[target].push(id);
                }
                //判断是否全选中了
                var len = $scope.tagsResult.length;
                var l=0;
                for(var k=0;k<len;k++){
                    if($scope.formData.targetTags.indexOf($scope.tagsResult[k].tagId) != -1){
                        l++;
                    };
                }
                //相等说明全选了,后台数组为空
                if(l==len){
                    $scope.formData.targetTags=[];
                };
                //删除父级
            }else if(action == 'remove' && pdx != -1){

                $scope.targetTag.splice(pdx,1);//删除前端数组中的该父ID
                var children = the.parentItem.list;
                for(var i=0;i<children.length;i++){
                    var cdx = $scope.targetTag.indexOf(children[i].tagId);
                    if(cdx != -1){//说明前台子级中有该项是被选中的,删除。
                        $scope.targetTag.splice(cdx,1);
                    } ;
                }

                //删除已选结果里的该父级以及子级结果
                for(var x=0;x<$scope.tagSelected.length;x++){
                    if($scope.tagSelected[x].tagId == id){
                        $scope.tagSelected.splice(x,1);//删除;
                        break;
                    };
                }

                //得到后台数组的长度，如果后台数据长度为0，则说明之前是全选上的，反之则不是。
                var len = $scope.formData.targetTags.length;
                //说明之前是非全选状态
                if(len>0){
                    //说明后台数组中含有该父ID，删除
                    var d =$scope.formData[target].indexOf(id);
                    if(d != -1){
                        $scope.formData[target].splice(d,1);
                    };
                    //说明之前是全选状态，则需要将除本级父ID的其他父ID都添加到后台数组中
                }else{
                    if($scope.formData.targetTags.length == 0){//全选
                        for(var p=0;p<$scope.tagsResult.length;p++){
                            var pid=$scope.tagsResult[p].tagId;
                            if(pid != id){
                                $scope.formData.targetTags.push(pid);
                            };
                        };
                    };
                };
                //console.log($scope.targetTag)
            };

        },
        checkChild:function ($scope,$event,target,id,the) {
            var checkbox = $event.target;
            var action = (checkbox.checked ? 'add' : 'remove');
            var idx = $scope.targetTag.indexOf(id);//该ID在前端数组中的索引                
            var parent = the.parentItem;
            var childList = parent.list;
            var pid = parent.tagId;

            //选中
            if(action == 'add'){
                //只有一个元素的情况
                if(childList.length == 1){
                    //console.log("添加")
                    $scope.targetTag.push(pid);//添加到前端数组中
                    $scope.targetTag.push(id);//添加到后端数组中
                    //添加到已选框
                    //console.log($scope.tagSelected)
                    var p = {};
                    p.tagId = pid;
                    p.tagName = parent.tagName;
                    p.list = [];
                    var c = {};
                    c.tagId = id;
                    c.tagName = the.childItem.tagName;
                    p.list.push(c);
                    $scope.tagSelected.push(p);
                    //console.log($scope.targetTag)
                }else{
                    $scope.targetTag.push(id);
                    //判断同级的其他子集是否选中，
                    var isAll = true;
                    for(var i=0;i<childList.length;i++){
                        if($scope.targetTag.indexOf(childList[i].tagId) == -1){//没有全选中
                            isAll = false;
                            break;
                        };
                    }

                    if(isAll){//全选
                        $scope.halfTagData[pid.toString()] = false;
                        if($scope.targetTag.indexOf(pid) == -1){
                            $scope.targetTag.push(pid);//将父集ID放进前端数组中，这样父集就被选中了
                        }
                        if($scope.formData[target].indexOf(pid) == -1){
                            $scope.formData[target].push(pid);//将父集ID放进后台数组中
                        }

                        for(var i=0;i<childList.length;i++){//将后台数组中含有的子集全删除
                            var cdx = $scope.formData[target].indexOf(childList[i].tagId);
                            if(cdx!=-1){//后台数组结果中含有的子集ID都删除
                                $scope.formData[target].splice(cdx,1);
                            };
                        }
                        //判断父级是否全选中
                        var len=$scope.tagsResult.length;
                        var l=0;
                        for(var m=0;m<len;m++){
                            if($scope.formData.targetTags.indexOf($scope.tagsResult[m].tagId)!=-1){
                                l++;
                            };
                        }
                        if(l==len){//所有父级都选中了,清空后台数组
                            $scope.formData.targetTags=[];
                        };
                    }else{//非全选
                        $scope.halfTagData[pid.toString()] = true;
                        if($scope.formData[target].indexOf(id) == -1){
                            $scope.formData[target].push(id);//将当前选中的ID赋进后台数组中
                        } ;

                    }
                    //判断已选结果是否存在
                    var isResultExsit = false;
                    var e = 0;
                    for(var m=0;m<$scope.tagSelected.length;m++){
                        if($scope.tagSelected[m].tagId == the.parentItem.tagId){
                            isResultExsit = true;
                            e=m;
                            break;
                        }
                    }
                    //不存在，则是第一次添加
                    if(!isResultExsit){
                        var p = {};
                        p.tagId = parent.tagId;
                        p.tagName = parent.tagName;
                        p.list = [];
                        var c = {};
                        c.tagId = id;
                        c.tagName = the.childItem.tagName;
                        p.list.push(c);
                        $scope.tagSelected.push(p);
                        e = $scope.tagSelected.length-1;
                    }else{
                        var c = {};
                        c.tagId = id;
                        c.tagName = the.childItem.tagName;
                        $scope.tagSelected[e].list.push(c);
                    }
                }


                //删除
            }else if(action == 'remove' ){
                //console.log("remove")
                var pdx = $scope.targetTag.indexOf(pid);
                if(the.parentItem.list.length == 1){
                    $scope.halfTagData[pid.toString()] = false;
                    //删除已选框结果
                    for(var m=0;m<$scope.tagSelected.length;m++){
                        if($scope.tagSelected[m].tagId == parent.tagId){
                            $scope.tagSelected.splice(m,1);
                            break;
                        }
                    }
                    //删除后台数组含有的对应结果
                    var idx = $scope.formData.targetTags.indexOf(parent.tagId);
                    $scope.formData.targetTags.splice(idx,1);
                    //删除前端数组中该父子ID
                    var idxs = $scope.targetTag.indexOf(parent.tagId);
                    $scope.targetTag.splice(idxs,1);
                    var idxson = $scope.targetTag.indexOf(id);
                    $scope.targetTag.splice(idxson,1);
                }else{
                    //说明之前该级对应的父级是全选中的哟,此时后台数组中只有父级ID
                    if(pdx != -1){
                        $scope.halfTagData[pid.toString()] = true;
                        $scope.targetTag.splice(pdx,1);//删除前端数组中父ID 
                        idx = $scope.targetTag.indexOf(id);//此时在前端数组中的索引
                        $scope.targetTag.splice(idx,1);//删除前端数组中该ID       		 

                        //删除已选框结果
                        for(var m=0;m<$scope.tagSelected.length;m++){
                            if($scope.tagSelected[m].tagId == parent.tagId){
                                var child = $scope.tagSelected[m].list;
                                for(var n=0;n<child.length;n++){
                                    if(child[n].tagId == id){
                                        $scope.tagSelected[m].list.splice(n,1);
                                        break;
                                    }
                                }
                            }
                        }

                        //所有父级全选，删除一个子级的元素后，则后台数组显示该级的所有子标签和其他所有父级标签
                        if($scope.formData.targetTags.length == 0){
                            pid = the.parentItem.tagId;
                            for(var p=0;p<$scope.tagsResult.length;p++){
                                var parentId=$scope.tagsResult[p].tagId;

                                if(parentId != pid){
                                    $scope.formData.targetTags.push(parentId);
                                }else{
                                    var child=$scope.tagsResult[p].list;
                                    for(var q=0;q<child.length;q++){
                                        if(q.tagId != id){
                                            $scope.formData.targetTags.push(child[q].tagId);
                                        }
                                    }
                                }
                            }
                        }else{
                            var d = $scope.formData[target].indexOf(pid);
                            if(d != -1){
                                $scope.formData[target].splice(d,1);//删除后台数组中的父ID
                            }
                            //添加其他子级被选中的id到后台数组中
                            for(var i=0; i<childList.length; i++){
                                if(id != childList[i].tagId){
                                    $scope.formData[target].push(childList[i].tagId);
                                }
                            }
                        }



                        //说明之前是部分选中的哟
                    }else{
                        $scope.targetTag.splice(idx,1);//前端数组中删除该ID
                        //判断是否是最后一个
                        var isLast = true;
                        for(var i=0;i<childList.length;i++){
                            if($scope.targetTag.indexOf(childList[i].tagId) != -1){
                                isLast = false;
                                break;
                            }
                        }
                        var dx = $scope.formData[target].indexOf(id);//该ID在后台数组中的索引
                        if(dx != -1){
                            $scope.formData[target].splice(dx,1);//删除后台数组中的该ID
                        }

                        //如果是该checkbox是最后一个被选中的，后台数组中删除父ID
                        if(isLast){
                            $scope.halfTagData[pid.toString()] = false;
                            var d = $scope.formData[target].indexOf(pid);
                            if(d != -1){
                                $scope.formData[target].splice(d,1);
                            }
                            //删除已选框结果
                            for(var m=0;m<$scope.tagSelected.length;m++){
                                $scope.tagSelected.splice(m,1);
                            }
                        }else{//已选框里只删除当前子级标签
                            $scope.halfTagData[pid.toString()] = true;
                            //删除已选框结果
                            for(var m=0;m<$scope.tagSelected.length;m++){
                                if($scope.tagSelected[m].tagId == parent.tagId){
                                    var child = $scope.tagSelected[m].list;
                                    for(var n=0;n<child.length;n++){
                                        if(child[n].tagId == id){
                                            $scope.tagSelected[m].list.splice(n,1);
                                        }
                                    }
                                }
                            }
                        }

                    }
                }

            }
        },
        backTag:function ($scope,result) {
            $scope.targetTag = [];
            $scope.tagSelected = [];
            if( !$scope.formData.targetTags||$scope.formData.targetTags.length == 0){//说明也是全选
                $scope.tagSelected = result;//已选框全显示
                for(var i=0;i<result.length;i++){
                    $scope.halfTagData[result[i].tagId.toString()] = false;//去掉部分选中样式
                    $scope.targetTag.push(result[i].tagId);
                    var child = result[i].list;
                    for(var j=0;j<child.length;j++){
                        $scope.targetTag.push(child[j].tagId);
                    }
                }
            }else{

                var array = $scope.formData.targetTags;
                for(var i=0;i<array.length;i++){//之所以有这步是因为直接赋给targetTag是赋的引用
                    $scope.targetTag.push(parseInt(array[i]));
                }
                //$scope.targetTag = array;//$scope.formData.targetTags;
                for(var i=0;i<result.length;i++){//遍历父级
                    var idx = $scope.targetTag.indexOf(result[i].tagId);
                    if(idx != -1){//说明是全选呢,将对应的子级标签全选上
                        $scope.halfTagData[result[i].tagId.toString()] = false;//去掉部分选中样式
                        var p = {};
                        p.tagId = result[i].tagId;//赋父id
                        p.tagName = result[i].tagName;//赋父名字
                        p.list=[];
                        var childList = result[i].list;
                        for(var j=0;j<childList.length;j++){
                            var cid = childList[j].tagId;
                            if( $scope.targetTag.indexOf(cid) == -1){
                                $scope.targetTag.push(cid);
                                var c = {};
                                c.tagId = cid;
                                c.tagName = childList[j].tagName;
                                p.list.push(c);//子级赋进父级的list里
                            }
                        }
                        $scope.tagSelected.push(p);
                        //前端数组中不含有该父级，再看看有没有子集
                    }else{
                        var childList = result[i].list;
                        for(var m=0;m<childList.length;m++){//循环当前子级所有标签                	              				
                            var d = $scope.targetTag.indexOf(childList[m].tagId);
                            //前端数组中有该子集的ID，则需将对应的父级加进已选
                            if(d != -1){
                                $scope.halfTagData[result[i].tagId.toString()] = true;//部分选中
                                //不是第一次添加到已选
                                if($scope.tagSelected.length>0){
                                    for(var t = 0; t< $scope.tagSelected.length;t++){
                                        //tagSelected里父级tagId已经存在了
                                        if($scope.tagSelected[t].tagId == result[i].tagId){
                                            var c = {};
                                            c.tagId = childList[m].tagId;
                                            c.tagName = childList[m].tagName;
                                            $scope.tagSelected[t].list.push(c);
                                            break;
                                            //不存在，要重新加一个
                                        }else{
                                            var p={};
                                            p.tagId = result[i].tagId;
                                            p.tagName = result[i].tagName;
                                            p.list=[];
                                            var c = {};
                                            c.tagId = childList[m].tagId;
                                            c.tagName = childList[m].tagName;
                                            p.list.push(c);
                                            $scope.tagSelected.push(p);
                                            break;
                                        }
                                    }
                                    //第一次给已选添加
                                }else{
                                    var p = {};
                                    p.tagId = result[i].tagId;
                                    p.tagName = result[i].tagName;
                                    p.list=[];
                                    var c = {};
                                    c.tagId = childList[m].tagId;
                                    c.tagName = childList[m].tagName;
                                    p.list.push(c);
                                    $scope.tagSelected.push(p);
                                }
                            }
                        }
                    }
                }
            }
        }
        
        

    };


    return PromotionGroupManager;
}]);
